package summary;

import java.util.Iterator;
import java.util.Vector;

public class NeighborInstantStatistics {
	
	public long second;
	public long skew;
	
	public NeighborInstantStatistics(InstantData data,int id1,int id2) {
		
		calculateSkews(data.getData(),id1,id2);		
								
		this.second = data.second;
	}
	
	public long getSkew(){
		return skew;
	}
	
	private void calculateSkews(Vector<NodeData> nodes,int id1,int id2){
			
		long clock1 = 0,clock2 = 0;
		
		for (Iterator<NodeData> iterator = nodes.iterator(); iterator.hasNext();) {
			NodeData nodeData = (NodeData) iterator.next();
			if(nodeData.id == id1)
				clock1 = nodeData.clock;
			else if (nodeData.id == id2)
				clock2 = nodeData.clock;
		}
		
		if(clock1 ==0 || clock2 == 0){
			skew = 0;
			return;
		}
		
		long diff = Math.abs(clock1-clock2); 
		
		if(diff > 0x0F000000L){
			long result = 0x0FFFFFFFFL;
			
			if(clock1 > clock2){
				result -= clock1;
				result += clock2;		
			}
			else{
				result -= clock2;
				result += clock1;
			}
							
			diff = result;
		}
		skew = diff;
	}
}
